exam_data <- read.csv("data/Exam_data.csv")Hands-on_Ex03
Import package
Interactive Data Visualisation
Import data
Interactive graph with ggiraph,tooltip
Tooltip effect with tooltip aesthetic
- Tooltip: show label when mouse scroll on it
- histodot: dot stacks vertically
- svg: Scalable Vector Graphics(adjust size so can with good resolution) for html page
- ratio of 0.618 is a recommended ratio for visually pleasing
plot <- ggplot(data=exam_data,aes(x=MATHS))+
geom_dotplot_interactive(aes(tooltip=ID),
stackgroups=TRUE,
binwidth=1,
method='histodot')+
scale_y_continuous(NULL,breaks=NULL)
girafe(
ggobj=plot,
width_svg = 6,
height_svg = 6*0.618
)Displaying multiple information
- Use “$” to extract specific column from dataframe
- “paste0” to concatenate string
exam_data$tooltip <- c(paste0(
"Name =", exam_data$ID, "\n Class =", exam_data$CLASS
))
plot <- ggplot(data=exam_data,aes(x=MATHS))+
geom_dotplot_interactive(aes(tooltip=exam_data$tooltip),
stackgroups=TRUE,
binwidth=1,
method='histodot')+
scale_y_continuous(NULL,breaks=NULL)
girafe(
ggobj=plot,
width_svg = 8,
height_svg = 8*0.618
)Warning: Use of `exam_data$tooltip` is discouraged.
ℹ Use `tooltip` instead.
Customising Tooltip
Use opts_tooltip() to customize interactive label s font and color
tooltip_css <- "background-color:white;
font-style:bold; color:blue;"
plot <- ggplot(data=exam_data,aes(x=MATHS))+
geom_dotplot_interactive(aes(tooltip=ID),
stackgroups=TRUE,
binwidth=1,
method="histodot")+
scale_y_continuous(NULL,breaks=NULL)
girafe(
ggobj=plot,
width_svg = 6,
height_svg = 6*0.618,
options = list(
opts_tooltip(
css=tooltip_css))
)Displaying statistics
accuracy: 0.01 means to round to two decimal places
ymax: upper limit of 90% interval of y, y: average
scales::number is to format value with accuracy (0.01)
after_stat:tooltip applied after calculating statistic values
fun.data: summary statistics, mean_se:calculate mean and standard error
tooltip <- function(y,ymax,accuracy=0.001){
mean <- scales::number(y,accuracy=accuracy)
se <- scales::number(ymax-y,accuracy = accuracy)
paste0("Mean math scores",mean," +/- ",se)
}
gg_point <- ggplot(data=exam_data,aes(x=RACE)
)+
stat_summary(aes(y=MATHS,
tooltip=after_stat(
tooltip(y,ymax))),
fun.data="mean_se",
geom=GeomInteractiveCol,
fill="lightblue"
)+
stat_summary(aes(y=MATHS),
fun.data="mean_se",
geom="errorbar",width=0.2,size=0.2)Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
girafe(
ggobj=gg_point,
width_svg = 8,
height_svg = 8*0.6188)Highlight effect
data_id allows highlighting effect on dotplot with defined category, default setting is filling with orange color.
plot <- ggplot(data=exam_data,aes(x=MATHS))+
geom_dotplot_interactive(aes(data_id=CLASS),
stackgroups=TRUE,
binwidth=1,
method="histodot")+
scale_y_continuous(NULL,breaks=NULL)
girafe(
ggobj=plot,
width_svg = 6,
height_svg=6*0.618
)Changing hovering color setting
opacity: changing the transparency, opacity=0.2 means with 80% transparency
plot <- ggplot(data=exam_data,aes(x=MATHS))+
geom_dotplot_interactive(aes(data_id=CLASS),
stackgroups=TRUE,
binwidth=1,
method="histodot")+
scale_y_continuous(NULL,breaks=NULL)
girafe(
ggobj=plot,
width_svg = 6,
height_svg=6*0.618,
options = list(
opts_hover(css="fill: #202020;"),
opts_hover_inv(css="opacity:0.1;")
)
)Combine tooltip(label) and hovering effect
plot <- ggplot(data=exam_data,aes(x=MATHS))+
geom_dotplot_interactive(
aes(tooltip=CLASS,data_id=CLASS),
stackgroups=TRUE,
binwidth=2,
method="histodot"
)+
scale_y_continuous(NULL,breaks=NULL)
girafe(
ggobj=plot,
width_svg=6,
height_svg=6*0.618,
options = list(
opts_hover(css="fill:#202020;"),
opts_hover_inv(css="opacity:0.2;")
)
)Insert link on the graph
- add onclick column to exam_data
- sprintf: combine new string with URL
- “%s%s”, first %s is the link, second %s is new adding string
- as.character: convert to character(string) type, and adding to URL
exam_data$onclick <- sprintf("window.open(\"%s%s\")",
"https://www.moe.gov.sg/schoolfinder?journey=Primary%20school",
as.character(exam_data$ID))
plot <- ggplot(data=exam_data,aes(x=MATHS))+
geom_dotplot_interactive(
aes(onclick=onclick),
stackgroups=TRUE,
binwidth=2,
method="histodot"
)+
scale_y_continuous(NULL,breaks=NULL)
girafe(
ggobj=plot,
width_svg=6,
height_svg=6*0.618
)Coordinate multiple views
- opts_hover: setting about hovering point
- opts_hover_inv: setting for other part when hovering
plot1 <- ggplot(data=exam_data,aes(x=MATHS))+
geom_dotplot_interactive(
aes(data_id=ID),
stackgroups=TRUE,
binwidth=2,
method="histodot"
)+
coord_cartesian(xlim=c(0,100))+
scale_y_continuous(NULL,breaks=NULL)
plot2 <- ggplot(data=exam_data,aes(x=ENGLISH))+
geom_dotplot_interactive(
aes(data_id=ID),
stackgroups=TRUE,
binwidth=2,
method="histodot"
)+
coord_cartesian(xlim=c(0,100))+
scale_y_continuous(NULL,breaks=NULL)
girafe(code = print(plot1+plot2),
width_svg=6,
height_svg=3,
options=list(
opts_hover(css="fill: #202020;"),
opts_hover_inv(css="opacity:0.2;")
))Interactive graphs by plotly
Interactive scatter plot
#|message: false
plot_ly(data = exam_data,
x = ~MATHS,
y = ~ENGLISH)No trace type specified:
Based on info supplied, a 'scatter' trace seems appropriate.
Read more about this trace type -> https://plotly.com/r/reference/#scatter
No scatter mode specifed:
Setting the mode to markers
Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
Adding visual effect
plot_ly(data= exam_data,
x= ~MATHS,
y= ~ENGLISH,
color= ~RACE)Coordinate multiple views
- highlight_key: create shared data for two plots
- subplot: coordinate two plots together
d <- highlight_key(exam_data)
p1 <- ggplot(data=d,aes(x=MATHS,y=ENGLISH))+
geom_point(size=2)+
coord_cartesian(xlim=c(0,100),ylim=c(0,100))
p2 <- ggplot(data=d,aes(x=MATHS,y=SCIENCE))+
geom_point(size=2)+
coord_cartesian(xlim=c(0,100),ylim=c(0,100))
subplot(ggplotly(p1),ggplotly(p2))Interactive graphs by ggplotly
plot <- ggplot(data=exam_data,aes(x=MATHS,y=ENGLISH))+
geom_point(size=2)+
coord_cartesian(xlim=c(0,100),ylim=c(0,100))
ggplotly(plot)Data table
class=“compact” means to have compact layout for the table
DT::datatable(exam_data,class="compact")Link table with graph
highlight_key: assign identifier to dataframe
plotly_selected: track corresponding point when mouse click on table row
crosstalk: combine table and graph
bscols: create layout with two elements
data <- highlight_key(exam_data)
p <- ggplot(data,aes(ENGLISH,MATHS))+
geom_point(siz=1)+
coord_cartesian(xlim=c(0,100),ylim=c(0,100))Warning in geom_point(siz = 1): Ignoring unknown parameters: `siz`
gg <- highlight(ggplotly(p),"plotly_selected")
crosstalk::bscols(gg,DT::datatable(data),widths = 5)Setting the `off` event (i.e., 'plotly_deselect') to match the `on` event (i.e., 'plotly_selected'). You can change this default via the `highlight()` function.
Animated Statistical Graphics
Import data
- mutate_each or mutate(across()):apply given function to selected columns
- %>% pass function/results to next step
- funs all all_of: create a list of function
- factor: turn column into categorical variable
col <- c("Country","Continent")
global <- read_xls("data/GlobalPopulation.xls",
sheet="Data") %>%
mutate(across(all_of(col), factor)) %>%
mutate(Year=as.integer(Year))Population bubble plot
static plot
ggplot(global,aes(x=Old,y=Young,size=Population,colour=Country))+
geom_point(alpha=0.7,show.legend = FALSE)+
scale_color_manual(values=country_colors)+
scale_size(range=c(2,10))+
labs(title='Year: {frame_time}',
x = '% Aged',
y = '% Young')
Animated plot with gganimate
ease_aes(): the moving way of plot element, default is linear
ggplot(global,aes(x=Old,y=Young,size=Population,colour=Country))+
geom_point(alpha=0.7,show.legend = FALSE)+
scale_color_manual(values=country_colors)+
scale_size(range=c(2,10))+
labs(title='Year: {frame_time}',
x = '% Aged',
y = '% Young')+
transition_time(Year)+
ease_aes("linear")
Animated plot with ggplotly
plot <- ggplot(global,aes(x=Old,y=Young,size=Population,colour=Country))+
geom_point(aes(size=Population, frame=Year),
alpha=0.7,show.legend = FALSE)+
scale_color_manual(values=country_colors)+
scale_size(range=c(2,10))+
labs( x = '% Aged',
y = '% Young')Warning in geom_point(aes(size = Population, frame = Year), alpha = 0.7, :
Ignoring unknown aesthetics: frame
ggplotly(plot)Warning in p$x$data[firstFrame] <- p$x$frames[[1]]$data: number of items to
replace is not a multiple of replacement length
Using plot_ly()
bp <- global %>%
plot_ly(x = ~Old,
y = ~Young,
size = ~Population,
color = ~Continent,
frame = ~Year,
text = ~Country,
hoverinfo = "text",
type = 'scatter',
mode = 'markers'
)
bp